package com.atguigu.study.exp;

import com.atguigu.study.resp.ResultData;
import com.atguigu.study.resp.ReturnCodeEnum;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResultData<String> exception(Exception e){
        System.out.println("#####come in GlobalExceptionHandler的exception方法，主管RuntimeException");
        log.error("全局异常信息",e.getMessage(),e);
        return ResultData.fail(ReturnCodeEnum.RC500.getCode(),e.getMessage());
    }

    @ExceptionHandler(BusinessException.class)
    public ResultData<String> handlerBusinessException(BusinessException e, HttpServletRequest request){
        System.out.println("#####come in GlobalExceptionHandler的handleBusinessException方法，主管自定义的BusinessException");
        log.error("请求：{}，发生异常：{}", request.getRequestURL(), e.getMessage(), e);
        return ResultData.fail(e.getCode(),e.getMessage()+"你的请求URL："+request.getRequestURL());
    }
}
